package org.wyn.action;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.bson.types.ObjectId;
import org.wyn.common.ResultType;
import org.wyn.mongodb.bean.Album;
import org.wyn.mongodb.common.ConnectionPool;
import org.wyn.service.IAlbumService;

import com.google.inject.Inject;
import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;

@Namespace("/album")
@ParentPackage("security")
public class AlbumAction extends WynActionSupport{
	/**
	 * 去相册页面
	 * @return
	 */
	@Action(value="goAlbum",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "album.ftl")})
	public String goAlbum(){
		albumList = iAlbumService.searchAlbum(getLoginUser().getUserName());
		return SUCCESS;
	}
	/**
	 * 显示一个特定的相册
	 * @return
	 */
	@Action(value="showOneAlbum",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "showOneAlbum.ftl")})
	public String showOneAlbum(){
		album = iAlbumService.findAlbumById(albumId);
		return SUCCESS;
	}
	@Action(value="goCreateAlbum",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "createAlbum.ftl")})
	public String goCreateAlbum(){
		return SUCCESS;
	}
	@Action(value="createAlbum",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "createAlbum.ftl")})
	public String createAlbum(){
		iAlbumService.createAlbum(getLoginUser().getUserName(),albumName);
		return SUCCESS;
	}
	/**
	 * 往相册中上传图片
	 */
	@Action(value="uploadImage")
	public void uploadImage(){
		iAlbumService.uploadImage(albumId,userImage,userImageContentType,userImageFileName);
	}
	/**
	 * 下载图片
	 * @return
	 */
	@Action(value="loadImage",results = {
			@Result(name = SUCCESS, type = ResultType.STREAM,params={"inputName","inputStream"})
			})
	public String loadImage(){
		try {
			DB db = ConnectionPool.getDB();
			GridFS gFs = new GridFS(db);
			GridFSDBFile file = gFs.find(new ObjectId(fileId));
			inputStream = file.getInputStream();
		} catch (Exception e) {
			System.out.println("指定图片:" + fileId + " 不存在!");
		}
		return SUCCESS;
	}
	/**
	 * 去上传头像页面
	 * @return
	 */
	@Action(value="goUploadPortrait",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "uploadPortrait.ftl")})
	public String goUploadPortrait(){
		return SUCCESS;
	}
	@Action(value="uploadPortrait")
	public void uploadPortrait(){
		iAlbumService.uploadPortrait(getLoginUserName(),userImage,userImageContentType,userImageFileName);
	}
	private String albumName;
	private String albumId;
	private Album album;
	private List<Album> albumList;
	/**上传图片参数*/
	private File userImage;
	private String userImageContentType;
	private String userImageFileName;
	/**下载图片参数*/
	private String fileId;
	@Inject
	private IAlbumService iAlbumService;
	private static final long serialVersionUID = 1L;
	public String getFileId() {
		return fileId;
	}
	public void setFileId(String fileId) {
		this.fileId = fileId;
	}
	private InputStream inputStream;
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getAlbumName() {
		return albumName;
	}
	public void setAlbumName(String albumName) {
		this.albumName = albumName;
	}
	public IAlbumService getiAlbumService() {
		return iAlbumService;
	}
	public void setiAlbumService(IAlbumService iAlbumService) {
		this.iAlbumService = iAlbumService;
	}
	public List<Album> getAlbumList() {
		return albumList;
	}
	public void setAlbumList(List<Album> albumList) {
		this.albumList = albumList;
	}
	public String getAlbumId() {
		return albumId;
	}
	public void setAlbumId(String albumId) {
		this.albumId = albumId;
	}
	public Album getAlbum() {
		return album;
	}
	public void setAlbum(Album album) {
		this.album = album;
	}
	public File getUserImage() {
		return userImage;
	}
	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}
	public String getUserImageContentType() {
		return userImageContentType;
	}
	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}
	public String getUserImageFileName() {
		return userImageFileName;
	}
	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

}
